//
//  SXTNextView.m
//  SXT
//
//  Created by 赵金鹏 on 16/8/15.
//  Copyright © 2016年 赵金鹏. All rights reserved.
//

#import "SXTNextView.h"

@interface SXTNextView()<UITextFieldDelegate>
@property (strong, nonatomic)   UILabel *tostLabel;              /** 提示验证码发送到哪个手机号中 */
@property (strong, nonatomic)   UITextField *verficationText;              /** 输入验证码的text */
@property (strong, nonatomic)   UIButton *showTime;              /** 显示剩余等待时间的button */
@property (strong, nonatomic)   UIButton *landingBtn;              /** 注册按钮 */
@property (strong, nonatomic)   UILabel *whiteBackLabe;              /** 白色背景 */
@property (strong, nonatomic)   UILabel *lineLabel;              /** 细分割线 */
@end


@implementation SXTNextView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tostLabel];
        [self addSubview:self.whiteBackLabe];
        [self addSubview:self.verficationText];
        [self addSubview:self.showTime];
        [self addSubview:self.landingBtn];
        [self addSubview:self.lineLabel];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    __weak typeof (self) weakSelf = self;
    [_tostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@35);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.right.equalTo(weakSelf);
    }];
    
    [_whiteBackLabe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tostLabel.mas_bottom);
        make.height.equalTo(@43);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
    }];
    
    [_verficationText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.equalTo(weakSelf.tostLabel.mas_bottom);
        make.right.equalTo(weakSelf.mas_right).offset(-100);
        make.height.equalTo(@43);
    }];
    
    [_showTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.verficationText.mas_right);
        make.right.equalTo(weakSelf.mas_right);
        make.top.equalTo(weakSelf.tostLabel.mas_bottom);
        make.height.equalTo(@43);
    }];
    
    [_landingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@35);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.top.equalTo(weakSelf.verficationText.mas_bottom).offset(16);
    }];
    
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.whiteBackLabe.mas_top).offset(5);
        make.bottom.equalTo(weakSelf.whiteBackLabe.mas_bottom).offset(-5);
        make.left.equalTo(weakSelf.verficationText.mas_right);
        make.width.equalTo(@1);
    }];
}

- (void)setPhoneNumText:(NSString *)phoneNumText{
    _phoneNumText = phoneNumText;
    _tostLabel.attributedText = [self makeTostAttributed];
}
#pragma mark - 初始化控件
- (UILabel *)tostLabel{
    if (!_tostLabel) {
        _tostLabel = [[UILabel alloc]init];
        _tostLabel.font = [UIFont systemFontOfSize:12.0];
    }
    return _tostLabel;
}

- (UILabel *)whiteBackLabe{
    if (!_whiteBackLabe) {
        _whiteBackLabe = [[UILabel alloc]init];
        _whiteBackLabe.backgroundColor = [UIColor whiteColor];
        _whiteBackLabe.layer.borderColor = RGB(188, 186, 192).CGColor;
        _whiteBackLabe.layer.borderWidth = 1.0;
    }
    return _whiteBackLabe;
}

- (UITextField *)verficationText{
    if (!_verficationText) {
        _verficationText = [[UITextField alloc]init];
        _verficationText.delegate = self;
        _verficationText.placeholder = @"请输入验证码";
        _verficationText.keyboardType = UIKeyboardTypeNumberPad;
        [_verficationText addTarget:self action:@selector(verficationFieldChangeText:) forControlEvents:(UIControlEventAllEditingEvents)];
    }
    return _verficationText;
}

- (UIButton *)showTime{
    if (!_showTime) {
        _showTime = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _showTime.titleLabel.textAlignment = NSTextAlignmentCenter;
        _showTime.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [_showTime addTarget:self action:@selector(startTimeRequest) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _showTime;
}

- (UIButton *)landingBtn{
    if (!_landingBtn) {
        _landingBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_landingBtn setTitle:@"注 册" forState:(UIControlStateNormal)];
        [_landingBtn setTitleColor:RGB(132, 132, 132) forState:(UIControlStateNormal)];
        [_landingBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        _landingBtn.selected = NO;
        _landingBtn.backgroundColor = RGB(229, 229, 229);
        _landingBtn.layer.masksToBounds = YES;
        _landingBtn.layer.cornerRadius = 5.0;
        [_landingBtn addTarget:self action:@selector(landingBtnMethod) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _landingBtn;
}

- (UILabel *)lineLabel{
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc]init];
        _lineLabel.backgroundColor = RGB(188, 186, 192);
    }
    return _lineLabel;
}
//重新获取验证码
- (void)startTimeRequest{
    if (_showTimeBlock) {
        _showTimeBlock();
    }
}
//注册按钮点击调用的方法
- (void)landingBtnMethod{
    if (_landingBtnBlock) {
        _landingBtnBlock(_verficationText.text);
    }
}


#pragma mark - textFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == _verficationText && range.location == 6) {
        return NO;
    }
    return YES;
}

#pragma mark - textfield修改文本内容时调用的方法
- (void)verficationFieldChangeText:(UITextField *)textFiedl{
    
    if (textFiedl.text.length == 6) {
        _landingBtn.backgroundColor = RGB(56, 166, 241);
        _landingBtn.userInteractionEnabled = YES;
        _landingBtn.selected = YES;
    }else{
        _landingBtn.backgroundColor = RGB(229, 229, 229);
        _landingBtn.userInteractionEnabled = NO;
        _landingBtn.selected = NO;
    }
}

#pragma mark - 制作属性字符串
//制作showtime显示内容的方法
- (NSMutableAttributedString *)makeShowBtnText:(NSInteger)time{
    NSDictionary *attributedDic1 = @{NSForegroundColorAttributeName:RGB(56, 166, 241)};
    NSString *string = [NSString stringWithFormat:@"%li",time];
    NSAttributedString *attributedString1 = [[NSAttributedString alloc]initWithString:string attributes:attributedDic1];
    
    NSDictionary *attributedDic2 = @{NSForegroundColorAttributeName:RGB(133, 133, 133)};
    NSAttributedString *attributedString2 = [[NSAttributedString alloc]initWithString:@"秒后重试" attributes:attributedDic2];
    
    NSMutableAttributedString *mutableAttributed = [[NSMutableAttributedString alloc]init];
    [mutableAttributed insertAttributedString:attributedString1 atIndex:0];
    [mutableAttributed insertAttributedString:attributedString2 atIndex:attributedString1.length];
    return mutableAttributed;
}
//制作提示文本属性字符串
- (NSMutableAttributedString *)makeTostAttributed{
    NSDictionary *attributedDic1 = @{NSForegroundColorAttributeName:RGB(133, 133, 133)};
    NSAttributedString *attributedString1 = [[NSAttributedString alloc]initWithString:@"验证码已发送到 " attributes:attributedDic1];
    
    NSDictionary *attributedDic2 = @{NSForegroundColorAttributeName:RGB(56, 166, 241)};
    NSString *string = [NSString stringWithFormat:@"+86 %@",_phoneNumText];
    NSAttributedString *attributedString2 = [[NSAttributedString alloc]initWithString:string attributes:attributedDic2];
    
    NSMutableAttributedString *mutableAttributed = [[NSMutableAttributedString alloc]init];
    [mutableAttributed insertAttributedString:attributedString1 atIndex:0];
    [mutableAttributed insertAttributedString:attributedString2 atIndex:attributedString1.length];
    return mutableAttributed;
}

#pragma mark - 倒计时
- (void)GCDTime{
    __block NSInteger time = 5;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        
        if (time < 2) {
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                _showTime.userInteractionEnabled = YES;
                NSDictionary *attributedDic1 = @{NSForegroundColorAttributeName:RGB(56, 166, 241)};
                NSAttributedString *attributedString1 = [[NSAttributedString alloc]initWithString:@"获取验证码" attributes:attributedDic1];
                [_showTime setAttributedTitle:attributedString1 forState:(UIControlStateNormal)];
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                _showTime.userInteractionEnabled = NO;
                [_showTime setAttributedTitle:[self makeShowBtnText:time] forState:(UIControlStateNormal)];
            });
            time --;
        }
    });
    dispatch_resume(timer);
}
@end
